package com.ys.zero.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.ys.zero.bean.Message;
import com.ys.zero.bean.Result;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.biz.MessageBiz;

@WebServlet("/msg/*")
public class MessageServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    
	private MessageBiz mbiz = new MessageBiz();
	
	
	protected void send(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, IllegalAccessException, InvocationTargetException {
		Message msg = new Message();
		BeanUtils.populate(msg,request.getParameterMap());
		try {
			mbiz.send(msg);
			toJson(response, new Result(1,"发送成功",msg));
		} catch (BizException e) {
			e.printStackTrace();
			toJson(response, new Result(0,e.getMessage()));
		}
	}
	
	protected void showMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String sendUid = request.getParameter("sendUid");
		String recvUid = request.getParameter("recvUid");
		List<Message> list = mbiz.showMsg(sendUid,recvUid);
		toJson(response, list);
	}
	
	
	protected void getRecvUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String recvUid = request.getParameter("recvUid");
		List<User> list = mbiz.getRecvUser(recvUid);
		toJson(response, list.get(0));
	}


}
